package cn.xinfei.xdecision.engine.runner.executor.handler;

import cn.xinfei.xdecision.common.model.component.strategyout.StrategyOutput;
import cn.xinfei.xdecision.common.utils.constant.runner.ParamTypeConst;
import cn.xinfei.xdecision.common.utils.util.runner.jeval.EvaluationException;
import cn.xinfei.xdecision.engine.runner.context.AmmunitionDepot;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.datax.DataRealCollectHandler;
import cn.xinfei.xdecision.engine.runner.utils.ExecuteUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class NodeOutputHandler {

    @Autowired
    protected DataRealCollectHandler dataRealCollectHandler;

    /*public Map<String, Object> executeOutputInContext(String nodeCode, List<StrategyOutput> strategyOutputList) throws EvaluationException {
        Map<String, Object> customOutput = new HashMap<>();
        if (!CollectionUtils.isEmpty(strategyOutputList)) {
            for (int j = 0; j < strategyOutputList.size(); j++) {
                StrategyOutput strategyOutput = strategyOutputList.get(j);
                if (!ExecuteUtils.judgeOutCondition(strategyOutput.getOutCondition())) {
                    continue;
                }
                String fieldCode = strategyOutput.getFieldCode();
                Object value = PipelineContextHolder.getValueByKey(fieldCode);

                Integer varValueType = strategyOutput.getVarValueType();
                Object newValue = DataValueCastUtils.cast(value, varValueType);
                if (null != newValue) {
                    customOutput.put(fieldCode, newValue);
//                    PipelineContextHolder.putInContext(nodeCode, fieldCode, newValue);
                }
            }
        }
        return customOutput;
    }*/


    /**
     * 只从上下文获取
     * @param nodeCode
     * @param strategyOutputList
     * @return
     * @throws EvaluationException
     */
    public Map<String, Object> getOutput(String nodeCode, List<StrategyOutput> strategyOutputList) throws EvaluationException {
        Map<String, Object> customOutput = new HashMap<>();
        if (!CollectionUtils.isEmpty(strategyOutputList)) {
            for (int j = 0; j < strategyOutputList.size(); j++) {
                StrategyOutput strategyOutput = strategyOutputList.get(j);
                if (!ExecuteUtils.judgeOutCondition(strategyOutput.getOutCondition())) {
                    continue;
                }
                String fieldCode = strategyOutput.getFieldCode();
                Object fieldValue = strategyOutput.getFieldValue();
                Integer valueType = strategyOutput.getValueType();
                //此时获取节点输出变量值时，只当成变量去取值即可
                Object value = AmmunitionDepot.getValueByKeyFromContext(ParamTypeConst.VARIABLE, fieldCode, fieldCode );
//                Object value = AmmunitionDepot.getValueByKey(ParamTypeConst.VARIABLE, fieldCode, fieldCode);
                if (value != null) {
                    customOutput.put(fieldCode, value);
//                    PipelineContextHolder.putInContext(nodeCode, fieldCode, value);
                }
            }
        }
        return customOutput;
    }

    /**
     * 支持从远程获取
     * @param nodeCode
     * @param strategyOutputList
     * @return
     * @throws EvaluationException
     */
    public Map<String, Object> setOutput(String nodeCode, List<StrategyOutput> strategyOutputList) throws EvaluationException {
        Map<String, Object> customOutput = new HashMap<>();
        if (!CollectionUtils.isEmpty(strategyOutputList)) {
            for (int j = 0; j < strategyOutputList.size(); j++) {
                StrategyOutput strategyOutput = strategyOutputList.get(j);
                if (!ExecuteUtils.judgeOutCondition(strategyOutput.getOutCondition())) {
                    continue;
                }
                String fieldCode = strategyOutput.getFieldCode();
                Object fieldValue = strategyOutput.getFieldValue();
                Integer valueType = strategyOutput.getValueType();
                boolean fixed = strategyOutput.isFixed();
                if (fixed) {
                    continue;//固定输出，不作处理
                }

                Object value = null;
                switch (valueType) {
                    case ParamTypeConst.CONSTANT:
                        //常量类型
                        value = AmmunitionDepot.getValueByKeyFromRemote(valueType, fieldCode, fieldValue + "");
                        break;
                    case ParamTypeConst.VARIABLE:
                        //变量类型
                        value = AmmunitionDepot.getValueByKeyFromRemote(valueType, fieldValue + "", fieldValue + "");
                        break;
                }
                if (value != null) {
                    customOutput.put(fieldCode, value);
                    PipelineContextHolder.putInContext(nodeCode, fieldCode, value);
                }
            }
        }
        return customOutput;
    }
}

